<template>
  <div class="main">
    <div class="header">
      <el-page-header content="搜索" title="返回首页" @back="goBack" />
    </div>
    <div class="search_input">
      <el-input
        v-model="search_input"
        placeholder="输入搜索内容"
        size="large"
        autofocus="true"
        @keyup.enter="search((content = null))"
      />
      <el-button
        style="margin-left: 10px"
        size="large"
        @click="search((content = null))"
        >搜索</el-button
      >
    </div>
    <div class="search_history">
      <ul class="search_history-list">
        <li
          v-for="(item, i) in search_history_list"
          :key="i"
          class="search_history-list-item"
        >
          <el-button round @click="search((content = item))"
            >{{item}}</el-button
          >
        </li>
      </ul>
    </div>
    <div class="search_result" v-if="result_show">
      <h1>查询“ {{ search_content }} ”结果如下:</h1>
      <EGraph
         ref="search_graph"
        :height="graph_height"
        :search_content="search_content"
        :main_node_num="10"
        :graph_event_handler="graph_event_handler"
      />
    </div>
  </div>
</template>

<script>
import { reactive, ref } from "@vue/reactivity";
import { useRouter, useRoute } from "vue-router";
import { getCurrentInstance, onMounted, onUnmounted } from "vue";
import EGraph from "@/components/main/EGraph.vue";
export default {
  components: { EGraph },
  setup() {
    const router = useRouter();
    const route = useRoute();
    function replaceWithQuery() {
      router.back(-1);
    }
    let {proxy}=getCurrentInstance();

    const graph_height = ref(450);
    const result_show = ref(true);
    let search_content = ref("");
    var search_history_list = reactive(new Set());
    const graph_event_handler = (search_content, node_num, node_total_num) => {
      search_history_list.add(search_content);
    };

    let search_input = ref("");
    let get = () => {
      var temp_list=proxy.$store.getters.getsearched_list;
      search_content.value=temp_list[1]
      temp_list[0].forEach(item => {
        search_history_list.add(item)
      });
      if (route.params.body) {
        search_input.value = JSON.parse(route.params.body);
        search_content.value = JSON.parse(route.params.body);
      }
      search(search_content.value);
    };
    let leave=()=>{
      var temp=Array();
      search_history_list.forEach(item => {
        temp.push(item)
      });
      proxy.$store.commit("setsearched_list", [temp,search_content.value]);
    };
    onUnmounted(leave)
    onMounted(get);

    const search = (content) => {
      if (content == null || content == undefined) {
        search_content.value = search_input.value;
      } else {
        search_content.value = content;
      }
      search_input.value=search_content.value
      // console.log(search_input.value);
      result_show.value = true;
    };
    const goBack = () => {
      // console.log("go back");
      replaceWithQuery();
    };
    return {
      goBack,
      search,
      search_input,
      graph_height,
      result_show,
      search_content,
      graph_event_handler,
      search_history_list,
    };
  },
};
</script>

<style scoped>
.main {
  box-sizing: border-box;
  width: 100vw;
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow-x: auto;
  overflow-y: auto;
}
.header {
  width: 100vw;
  padding: 17px 10px 38px 10px;
  box-sizing: border-box;
  height: 50px;
  background-color: rgba(241, 241, 241, 0.79);
}
.search_input {
  box-sizing: border-box;
  width: 55vw;
  align-self: center;
  margin-top: 45px;
  display: flex;
  flex-direction: row;
}
.search_history {
  margin-top: 10px;
  box-sizing: border-box;
  width: 75vw;
  align-self: center;
  box-shadow: 3px 4px 5px #f1f1f1;
  border: 1px solid rgba(236, 236, 236, 0.6);
  border-radius: 15px;
  display: flex;
  flex-direction: row;
}
.search_result {
  margin-top: 20px;
  box-sizing: border-box;
  width: 75vw;
  height: 65vh;
  align-self: center;
  overflow: hidden;
  box-shadow: 8px 10px 20px #dadada;
  border: 1px solid rgba(222, 222, 222, 0.6);
  border-radius: 15px;
  display: flex;
  flex-direction: column;
}
.search_result h1 {
  font-size: 17px;
  padding-left: 10px;
  border-left: 4px solid rgb(105, 105, 105);
  width: fit-content;
  align-self: left;
  margin-top: 20px;
  margin-left: 24px;
  color: rgb(69, 69, 69);
}

.search_history-list {
  height: 100%;
  padding: 10px;
  box-sizing: border-box;
  width: 95%;
  list-style: none;
  display: flex;
  flex-direction: row;
}
.search_history-list-item {
  width: max-content;
  margin-right: 5px;
  height: max-content;
}
</style>
